
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class StringExtractor {
    public static void main(String[] args) {
        String filePath = "path/to/source/file";

        try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
            String line;
            StringBuilder extractedString = new StringBuilder();
            State currentState = State.WAIT_INPUT;

            while ((line = reader.readLine()) != null) {
                int i = 0;
                while (i < line.length()) {
                    char c = line.charAt(i);

                    switch (currentState) {
                        case WAIT_INPUT:
                            if (c == '\"') {
                                currentState = State.SEARCH_START_QUOTE;
                            }
                            break;

                        case SEARCH_START_QUOTE:
                            if (c == '\"') {
                                currentState = State.FIND_STRING_CONTENT;
                            }
                            break;

                        case FIND_STRING_CONTENT:
                            if (c == '\"') {
                                System.out.println(extractedString.toString());
                                extractedString.setLength(0);
                                currentState = State.WAIT_INPUT;
                            } else if (c == '\\') {
                                currentState = State.HANDLE_ESCAPE_CHARACTER;
                            } else {
                                extractedString.append(c);
                            }
                            break;

                        case HANDLE_ESCAPE_CHARACTER:
                            if (c == '\"' || c == '\\') {
                                extractedString.append(c);
                            } else {
                                extractedString.append('\\').append(c);
                            }
                            currentState = State.FIND_STRING_CONTENT;
                            break;
                    }

                    i++;
                }
            }

            // 处理文件结束时还处于查找字符串内容状态的情况
            if (currentState == State.FIND_STRING_CONTENT) {
                System.out.println("字符串未结束： " + extractedString.toString());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private enum State {
        WAIT_INPUT,
        SEARCH_START_QUOTE,
        FIND_STRING_CONTENT,
        HANDLE_ESCAPE_CHARACTER
    }
}





